<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_Admin extends Controller_Home
{
    public function action_entry()
    {
            $this->network();
            $this->login();
    }
            
    public function close()
    {
        if(isset($_POST['exit']))
        {
            Cookie::delete('login');
            Cookie::delete('pasword');
            Cookie::delete('id');
            $this->redirect('Admin/entry');
        }
    }
    
    public function entry()
    {
        $adm = ORM::factory('admin')
            ->where('login', '=', Cookie::get('login','no user'))
            ->find();
        if(Cookie::get('login', 'no user')==$adm->login && Cookie::get('pasword')==$adm->password )
        {
          //  $content = View::factory('temp');
          //  $this->template->content = $content;
          //  $this->close();
        }
    }
    
    public function login()
    {
        $adm = ORM::factory('admin')
            ->where('login', '=', Cookie::get('login','no user'))
            ->find();
        if(Cookie::get('login', 'no user')==$adm->login && Cookie::get('pasword')==$adm->password )
        {
            $content = View::factory('temp/temp');
            $this->template->content = $content;
            $this->close();
        }
        else
        {
            $content = View::factory('other');
            $this->template->content = $content;  
            
            if(isset($_POST['ok']))
            {
                $login=Arr::get($_POST, 'login', '');
                $password=Arr::get($_POST, 'password', '');
                
                $admins = ORM::factory('admin')
                    ->where('login', '=', $login)
                    ->find();
                
                if($admins->loaded())
                {
                    if($login==$admins->login && $password==$admins->password)
                    {
                        Cookie::set('login', $_POST['login']);
                        Cookie::set('pasword', $_POST['password']);
                        Cookie::set('id', $admins->id);    
                        $content = View::factory('temp/temp');
                        $this->template->content = $content;
                        $this->close();
                        //$this->redirect('admin'); //перехід на сторінку адміна, якщо така буде
                        
                    }
                    else
                    {
                     //   $error = View::factory('error2');
                     //   $this->template->error = $error;
                    }
                }
                else
                {
                 //   $error = View::factory('error1');
                 //   $this->template->error = $error;
                }
            }
            
        }
    }

}